const util = require('../../../utils/util.js')
const {
  findMyPack
} = require('../../../api/geData.js')
let app = getApp()

Page({
  data: {
    stu_id: wx.getStorageSync("userInfo").cluesOrStuId,
    listCourse: [],
    limit: 12,
    listNo: 0,
    isEmpty: true,
    hasDate: false
  },
  onLoad(options) {
    wx.showLoading({
      mask: true
    })
    this.getMeal()
  },
  onShow()
  {
    app.globalData.stopMoreClick = false
  },
  // 导致我的课程闪退
  // onUnload(){
  //   wx.switchTab({
  //     url: '/pages/mine/mine',
  //   })
  // },
  getMeal() {
    let _this = this
    findMyPack(wx.getStorageSync("userInfo").cluesOrStuId).then(res => {
      wx.hideLoading()
      _this.solveData(res.myPackList)
      if (res.myPackList.length==0){
        _this.setData({
          hasDate: true
        })
      }
    })

  },
  solveData(list) {
    let lesson = []
    lesson = util.listSolve(list)
    this.setData({
      listCourse: lesson
    })
    // getTime().then(res => {
      let num = '',
        baifenbi = '',
        that = this,
        currentDay = wx.getStorageSync('time'),
        listCourse = this.data.listCourse;
      for (let i = 0; i < listCourse.length; i++) {
        if (listCourse[i].periods != null && listCourse[i].inPeriods != null) {
          num = listCourse[i].inPeriods / listCourse[i].periods
        } else {
          num = 0;
        }
        if (listCourse[i].status == 6) {
          if (currentDay >= listCourse[i].endtime) {
            baifenbi = 100
            // console.log(endtime)
          } else {
            if (num >= 1) {
              baifenbi = 100
            } else {
              baifenbi = (num * 100).toFixed(1)
            }
          }
        } else if (listCourse[i].status == 7) {
          // baifenbi = (num * 100).toFixed(1)
          baifenbi = 100
        } else {
          if (num >= 1) {
            baifenbi = 100
          } else {
            baifenbi = (num * 100).toFixed(1)
          }
        }
        var percent = 'listCourse[' + i + '].percent'
        that.setData({
          [percent]: baifenbi
        })
      }
    // })
  }
})